Repository navigation
fix(parse): reject incomplete format matches - #152
awais-ur-rehman wants to merge 1 commit into
Conversation
knowledgecode
left a comment
There was a problem hiding this comment.
Thanks for the PR! I agree this is a bug. I'll handle docs/ and skills/ myself after the merge.
A few changes before merging:
-
Rename
_errorto_mismatch.preparse()doesn't decide validity. The flag records a matching result, like_match. -
Set the flag in one place. Wrap the loop in an inner function that returns whether the whole pattern was matched: each
breakbecomesreturn false, except at..., which returnstrue. This way, a branch added later can't forget to set it. -
Keep the old
_indexfor a wildcard past the end of the input. With thestr.length > 0guard,preparse('2025-01-01', 'YYYY-MM-DD ')returns_index: 10, _length: 10. On master it returns11, 10, so code that checks_index === _lengthitself would start accepting this input. Advance_indexas before, then stop the loop.const matchPattern = () => { for (const token of pattern) { // ... } else if (token === str[0] || token === wildcard) { pr._index++; if (!str) { return false; } } // ... } return true; }; if (!matchPattern()) { pr._mismatch = true; }
I checked this sketch against all 19 cases in your test file, and they all give the results you expect.
-
Move the tests into
tests/preparse.spec.ts,tests/isValid.spec.ts, andtests/parse.spec.ts. Test files mirrorsrc/.
One note: this isn't quite free of breaking changes. Input that used to parse, such as parse('2025-01', 'YYYY-MM-DD'), now returns Invalid Date. No need to update the description, though.
Summary
Parsing
2025againstYYYY-MM-DDcurrently succeeds even though the month and day are absent. Record required-token matching failures inpreparseand reject them in the shared validator used byparseandisValid, while preserving parsed components and intentional short formats or ellipsis.PreparseResultgains an optional_error: truefield on mismatches; successful result shapes are unchanged.Breaking changes
None.
PreparseResultadds an optional error flag on failed matches.Testing
Added 20 regression cases, with nine cases failing before the fix. Full
npm testpasses all 2373 tests after rebuilding;npm run lint,npm run ts,npm run build, andgit diff --checkpass on Node 24/macOS. Other runtime/platform combinations were not exercised locally.Manual check:
isValid("2025", "YYYY-MM-DD")returns false andparsereturns an invalid Date; the same input withYYYYremains valid.AI assistance
OpenAI Codex assisted with investigation, implementation, regression tests, review, and validation.